// index.js
let myRequest = require('../../common/script/myRequest.js');
let api = require('../../common/script/api.js');
let login = require('../../common/script/login');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    height: app.globalData.ratio + 'px',
    ratioTop: app.globalData.ratioTop + 'px',
    ratioHeight: app.globalData.ratioHeight + 'px',
    logoHeight: app.globalData.ratio / 2 + 'px',
    isnoshowCard: true,
    memberData: null,
    couponNum: 0,
    listData: [],
    page: 1,
    limit: 10,
    isEnd: true,
    isWithout: true,
    http: api.http
  },
  getGoods() {
    const that = this;
    let data = {
      limit: that.data.limit,
      page: that.data.page,
      token: wx.getStorageSync('token'),
    }
    myRequest.EasyHttp.post(api.reqUrls.getList, data).then(res => {
      if (res.data.status == 1) {
        if (res.data.result.data.length == 0) {
          that.setData({
            isWithout: false
          })
        } else {
          that.setData({
            listData: res.data.result.data,
          })
        }

      }
    })
  },
  getData() {
    const that = this;
    let data = {
      token: wx.getStorageSync('token'),
    }
    wx.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.post(api.reqUrls.membercardShow, data).then(res => {
      wx.hideLoading();
      wx.stopPullDownRefresh();
      wx.hideNavigationBarLoading();
      if (res.data.status == 1) {
        that.setData({
          memberData: res.data.result.member_data,
          isnoshowCard: true,
          couponNum: res.data.result.can_use_coupon_count
        })
        wx.setStorageSync('memberData', res.data.result.member_data)
        wx.setStorageSync('couponNum', res.data.result.can_use_coupon_count)
      } else if (res.data.status == -2) {
        that.setData({
          isnoshowCard: false,
          memberData: null
        })
        wx.setStorageSync('memberData', null)
      } else if (res.data.status == -101 || res.data.status == -100) {
        login.loginByWeixin().then(res => {
          app.globalData.token = res.data.result.token
          that.getData()
        })
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'error',
          duration: 2000
        })
      }
    })
  },
  // 跳转到优惠券页面
  toCoupon() {
    wx.setStorageSync('refreshCoupon', true)
    wx.switchTab({
      url: '/pages/coupon/coupon'
    })
  },
  //商品详情
  toParticulars(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/goods/pages/goodsDetails/goodsDetails?id=${id}`
    })
  },
  //充值金额
  toTecharge() {
    const that = this;
    if (!that.data.isnoshowCard) {
      wx.showToast({
        title: '请绑定会员卡',
        icon: 'error',
        duration: 2000
      })
      return
    }
    wx.navigateTo({
      url: `/package/pages/recharge/recharge`,
    })
  },
  // 付款
  toPayment() {
    const that = this;
    if (!that.data.isnoshowCard) {
      wx.showToast({
        title: '请绑定会员卡',
        icon: 'error',
        duration: 2000
      })
      return
    }
    wx.navigateTo({
      url: `/package/pages/payment/payment`,
    })
  },
  //注册会员
  toAplicationCard() {
    wx.navigateTo({
      url: `/page/details/pages/aplicationCard/aplicationCard?type=true`
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const that = this;
    login.checkLogin().then(res => {
      that.getData()
      that.getGoods()
    }).catch(err => {
      console.log(11111)
      app.appCallBack = (e) => {
        console.log(55555)
        that.getData()
        that.getGoods()
      }
    })
  },
  // 绑定我的会员
  toSwapCard() {
    wx.navigateTo({
      url: `/package/pages/swapCard/swapCard?type=1`
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const that = this;
    const refresh = wx.getStorageSync('refresh');
    if (refresh) {
      wx.setStorageSync('refresh', false)
      that.getData()
    } else {
      let memberData = wx.getStorageSync('memberData')
      let couponNum = wx.getStorageSync('couponNum')
      that.setData({
        memberData: memberData,
        couponNum: couponNum,
        isnoshowCard: memberData == null ? false : true
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    const that = this;
    wx.showNavigationBarLoading()
    that.getData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    const that = this;
    if (!that.data.isEnd) {
      return
    }
    let data = {
      token: wx.getStorageSync('token'),
      limit: that.data.limit,
      page: that.data.page + 1,
    }
    wx.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.post(api.reqUrls.getList, data).then(res => {
      wx.hideLoading();
      if (res.data.status == 1) {
        if (res.data.result.data.length == 0) {
          wx.showToast({
            title: '没有更多了',
            icon: 'none',
            duration: 1000
          })
          that.setData({
            isEnd: false
          })
        } else {
          that.setData({
            listData: [...that.data.listData, ...res.data.result.data],
            page: that.data.page + 1
          })
        }
      }
    })
  },


  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage() {

  // }
})